package com.palandrodev.calsmart;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.palandrodev.calsmart.base.CompoundInterest;
import com.palandrodev.calsmart.util.Constants;
import com.palandrodev.calsmart.util.NumberTextWatcher;

public class CompoundInterestActivity extends Activity {
	private TextView cinterestViewHeading;
	private EditText cprincipleInput;
	private EditText croiInput;
	private EditText cdurationInput;
	private EditText cresultOutput;
	private Button ccalculateButton;

	public TextView getCinterestViewHeading() {
		return cinterestViewHeading;
	}

	public void setCinterestViewHeading(TextView cinterestViewHeading) {
		this.cinterestViewHeading = cinterestViewHeading;
	}

	public EditText getCprincipleInput() {
		return cprincipleInput;
	}

	public void setCprincipleInput(EditText cprincipleInput) {
		this.cprincipleInput = cprincipleInput;
	}

	public EditText getCroiInput() {
		return croiInput;
	}

	public void setCroiInput(EditText croiInput) {
		this.croiInput = croiInput;
	}

	public EditText getCdurationInput() {
		return cdurationInput;
	}

	public void setCdurationInput(EditText cdurationInput) {
		this.cdurationInput = cdurationInput;
	}

	public EditText getCresultOutput() {
		return cresultOutput;
	}

	public void setCresultOutput(EditText cresultOutput) {
		this.cresultOutput = cresultOutput;
	}

	public Button getCcalculateButton() {
		return ccalculateButton;
	}

	public void setCcalculateButton(Button ccalculateButton) {
		this.ccalculateButton = ccalculateButton;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compound_interest_view);
		initialize();
	}

	private void initialize() {
		setCinterestViewHeading((TextView) findViewById(R.id.compoundInterestViewHeading));
		setCprincipleInput((EditText) findViewById(R.id.cprincipleInput));
		setCroiInput((EditText) findViewById(R.id.croiInput));
		setCdurationInput((EditText) findViewById(R.id.cdurationInput));
		setCresultOutput((EditText) findViewById(R.id.cresultOutput));
		setCcalculateButton((Button) findViewById(R.id.ccalculateButton));

		cinterestViewHeading.setText("Compound Interest");
		cprincipleInput.setText("0.0");
		croiInput.setText("0.0");
		cdurationInput.setText("0.0");

		cprincipleInput.addTextChangedListener(new NumberTextWatcher(
				cprincipleInput));
		croiInput.addTextChangedListener(new NumberTextWatcher(croiInput));

		ccalculateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				float principle = 0.0f, interest = 0.0f, duration = 0.0f;
				try {
					principle = Float.valueOf(cprincipleInput.getText()
							.toString().replaceAll(",", ""));
					interest = Float.valueOf(croiInput.getText().toString()
							.replaceAll(",", ""));
					duration = Float.valueOf(cdurationInput.getText()
							.toString());

				} catch (Exception e) {
					Log.d(Constants.APP_NAME, e.getMessage());
				}
				NumberFormat nf = new DecimalFormat("#,###.00");
				CompoundInterest ci = new CompoundInterest(principle, duration,
						interest);

				String formattedInterest = nf.format(ci.calculate());
				cresultOutput.setText("$ " + formattedInterest);

			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.menu_settings:
			Intent i = new Intent(this, ShowSettingsActivity.class);
			startActivityForResult(i, Constants.RESULT_SETTINGS);
			break;

		}

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Constants.RESULT_SETTINGS:

			break;

		}

	}

}
